/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { Request, Response } from 'express';
import { ResponseItem } from '../../utils';
import type { address } from '@/pages/Financial/YzBond/index.d';

interface Address extends address {
  disabled?: boolean;
}
interface Address_Type {
  prov: Address[];
  city: Address[];
  area: Address[];
  street: Address[];
}
export default {
  //加盟商地址获取 id查询
  'POST /Inn/getChildrenArea': (req: Request, res: Response) => {
    const { id } = req.body;
    let data: address[] = [];
    const getAddress = (data: any, id: any) => {
      return data.filter((item: any) => item.pid == id);
    };
    const prov: address[] = [
      { id: '1', name: '上海市', code: '310000000000', pid: '0' },
      { id: '334', name: '浙江省', code: '330000000000', pid: '0' },
    ];
    const city: address[] = [
      { id: '2', name: '上海市', code: '310100000000', pid: '1' },
      { id: '336', name: '杭州市', code: '330100000000', pid: '334' },
    ];
    const area: address[] = [
      { id: '6', name: '长宁区', code: '310105000000', pid: '2' },
      { id: '7', name: '其他', code: '310106000000', pid: '2' },
      { id: '338', name: '拱墅区', code: '330105000000', pid: '336' },
    ];
    const street: address[] = [
      { id: '882431', name: '南京东路街道', code: '310101002000', pid: '6' },
      { id: '882423', name: '其他', code: '310101002001', pid: '6' },
      { id: '882668', name: '米市巷街道', code: '330105001000', pid: '338' },
    ];
    if (id == '0') {
      data = prov;
    } else if (id == '334' || id == '1') {
      data = getAddress(city, id);
    } else if (id == '336' || id == '2') {
      data = getAddress(area, id);
    } else if (id == '338' || id == '6') {
      data = getAddress(street, id);
    }

    const result: ResponseItem = {
      code: 0,
      msg: '成功',
      data: data,
    };
    return res.json(result);
  },

  //加盟商地址获取 id、code查询
  'POST /jdk/Area/getChildArea': (req: Request, res: Response) => {
    //
    const { id } = req.body;
    let data: Address[] = [];
    const addressMap: Address_Type = {
      prov: [
        {
          id: '1',
          pid: '0',
          name: '上海市',
          code: '310000000000',
        },
        {
          id: '334',
          pid: '0',
          name: '浙江省',
          code: '330000000000',
        },
        {
          id: '335',
          pid: '0',
          name: '江苏省',
          code: '320000000000',
        },
        {
          id: '681',
          pid: '0',
          name: '安徽省',
          code: '340000000000',
        },
        {
          id: '5302',
          pid: '0',
          name: '北京市',
          code: '110000000000',
        },
        {
          id: '5342',
          pid: '0',
          name: '天津市',
          code: '120000000000',
        },
        {
          id: '5382',
          pid: '0',
          name: '河北省',
          code: '130000000000',
        },
        {
          id: '5430',
          pid: '0',
          name: '山东省',
          code: '370000000000',
        },
        {
          id: '5477',
          pid: '0',
          name: '江西省',
          code: '360000000000',
        },
        {
          id: '5522',
          pid: '0',
          name: '福建省',
          code: '350000000000',
        },
        {
          id: '5565',
          pid: '0',
          name: '河南省',
          code: '410000000000',
        },
        {
          id: '5591',
          pid: '0',
          name: '湖北省',
          code: '420000000000',
        },
        {
          id: '5619',
          pid: '0',
          name: '湖南省',
          code: '430000000000',
        },
        {
          id: '5639',
          pid: '0',
          name: '广西壮族自治区',
          code: '450000000000',
        },
        {
          id: '5700',
          pid: '0',
          name: '重庆市',
          code: '500000000000',
        },
        {
          id: '5784',
          pid: '0',
          name: '四川省',
          code: '510000000000',
        },
        {
          id: '5824',
          pid: '0',
          name: '贵州省',
          code: '520000000000',
        },
        {
          id: '5846',
          pid: '0',
          name: '云南省',
          code: '530000000000',
        },
        {
          id: '5876',
          pid: '0',
          name: '广东省',
          code: '440000000000',
        },
        {
          id: '6166',
          pid: '0',
          name: '海南省',
          code: '460000000000',
        },
        {
          id: '6194',
          pid: '0',
          name: '山西省',
          code: '140000000000',
        },
        {
          id: '7603',
          pid: '0',
          name: '甘肃省',
          code: '620000000000',
        },
        {
          id: '7653',
          pid: '0',
          name: '宁夏回族自治区',
          code: '640000000000',
        },
        {
          id: '7661',
          pid: '0',
          name: '黑龙江省',
          code: '230000000000',
        },
        {
          id: '7769',
          pid: '0',
          name: '吉林省',
          code: '220000000000',
        },
        {
          id: '7869',
          pid: '0',
          name: '辽宁省',
          code: '210000000000',
        },
        {
          id: '8018',
          pid: '0',
          name: '内蒙古自治区',
          code: '150000000000',
        },
        {
          id: '8058',
          pid: '0',
          name: '青海省',
          code: '630000000000',
        },
        {
          id: '8068',
          pid: '0',
          name: '陕西省',
          code: '610000000000',
        },
        {
          id: '8240',
          pid: '0',
          name: '西藏自治区',
          code: '540000000000',
        },
        {
          id: '8242',
          pid: '0',
          name: '新疆维吾尔自治区',
          code: '650000000000',
        },
        {
          id: '882353',
          pid: '0',
          name: '台湾省',
          code: '710000000000',
        },
        {
          id: '882379',
          pid: '0',
          name: '香港特别行政区',
          code: '810000000000',
        },
        {
          id: '882398',
          pid: '0',
          name: '澳门特别行政区',
          code: '820000000000',
        },
      ],
      city: [
        { id: '2', name: '上海市', code: '310100000000', pid: '1' },
        { id: '682', name: '宁波市', code: '330200000000', pid: '334' },
        { id: '336', name: '杭州市', code: '330100000000', pid: '334' },
        { id: '337', name: '南京市', code: '320100000000', pid: '335' },
      ],
      area: [
        {
          id: '3',
          pid: '2',
          name: '黄浦区',
          code: '310101000000',
        },
        {
          id: '5',
          pid: '2',
          name: '徐汇区',
          code: '310104000000',
        },
        {
          id: '6',
          pid: '2',
          name: '长宁区',
          code: '310105000000',
        },
        {
          id: '7',
          pid: '2',
          name: '静安区',
          code: '310106000000',
        },
        {
          id: '8',
          pid: '2',
          name: '普陀区',
          code: '310107000000',
        },
        {
          id: '10',
          pid: '2',
          name: '虹口区',
          code: '310109000000',
        },
        {
          id: '11',
          pid: '2',
          name: '杨浦区',
          code: '310110000000',
        },
        {
          id: '12',
          pid: '2',
          name: '闵行区',
          code: '310112000000',
        },
        {
          id: '13',
          pid: '2',
          name: '宝山区',
          code: '310113000000',
        },
        {
          id: '14',
          pid: '2',
          name: '嘉定区',
          code: '310114000000',
        },
        {
          id: '15',
          pid: '2',
          name: '浦东新区',
          code: '310115000000',
        },
        {
          id: '16',
          pid: '2',
          name: '金山区',
          code: '310116000000',
        },
        {
          id: '17',
          pid: '2',
          name: '松江区',
          code: '310117000000',
        },
        {
          id: '18',
          pid: '2',
          name: '青浦区',
          code: '310118000000',
        },
        {
          id: '20',
          pid: '2',
          name: '奉贤区',
          code: '310120000000',
        },
        {
          id: '21',
          pid: '2',
          name: '崇明区',
          code: '310151000000',
        },
        {
          id: '338',
          pid: '336',
          name: '拱墅区',
          code: '330105000000',
        },
        {
          id: '339',
          pid: '336',
          name: '上城区',
          code: '330102000000',
        },
        {
          id: '341',
          pid: '336',
          name: '江干区',
          code: '330104000000',
        },
        {
          id: '342',
          pid: '336',
          name: '西湖区',
          code: '330106000000',
        },
        {
          id: '343',
          pid: '336',
          name: '滨江区',
          code: '330108000000',
        },
        {
          id: '344',
          pid: '336',
          name: '萧山区',
          code: '330109000000',
        },
        {
          id: '345',
          pid: '336',
          name: '余杭区',
          code: '330110000000',
        },
        {
          id: '346',
          pid: '336',
          name: '建德市',
          code: '330182000000',
        },
        {
          id: '347',
          pid: '336',
          name: '富阳区',
          code: '330111000000',
        },
        {
          id: '348',
          pid: '336',
          name: '临安区',
          code: '330112000000',
        },
        {
          id: '349',
          pid: '336',
          name: '桐庐县',
          code: '330122000000',
        },
        {
          id: '350',
          pid: '336',
          name: '淳安县',
          code: '330127000000',
        },
        {
          id: '923892',
          pid: '336',
          name: '临平区',
          code: '330113000000',
        },
        {
          id: '923893',
          pid: '336',
          name: '钱塘区',
          code: '330114000000',
        },
        {
          id: '549',
          pid: '337',
          name: '玄武区',
          code: '320102000000',
        },
        {
          id: '551',
          pid: '337',
          name: '秦淮区',
          code: '320104000000',
        },
        {
          id: '552',
          pid: '337',
          name: '建邺区',
          code: '320105000000',
        },
        {
          id: '553',
          pid: '337',
          name: '鼓楼区',
          code: '320106000000',
        },
        {
          id: '555',
          pid: '337',
          name: '浦口区',
          code: '320111000000',
        },
        {
          id: '556',
          pid: '337',
          name: '六合区',
          code: '320116000000',
        },
        {
          id: '557',
          pid: '337',
          name: '栖霞区',
          code: '320113000000',
        },
        {
          id: '558',
          pid: '337',
          name: '雨花台区',
          code: '320114000000',
        },
        {
          id: '559',
          pid: '337',
          name: '江宁区',
          code: '320115000000',
        },
        {
          id: '560',
          pid: '337',
          name: '溧水区',
          code: '320117000000',
        },
        {
          id: '561',
          pid: '337',
          name: '高淳区',
          code: '320118000000',
        },
        { id: '683', pid: '682', name: '海曙区', code: '330203000000' },
        { id: '700', pid: '682', name: '江北区', code: '330205000000' },
        { id: '709', pid: '682', name: '北仑区', code: '330206000000' },
        { id: '719', pid: '682', name: '镇海区', code: '330211000000' },
        { id: '726', pid: '682', name: '鄞州区', code: '330212000000' },
        { id: '749', pid: '682', name: '象山县', code: '330225000000' },
        { id: '768', pid: '682', name: '宁海县', code: '330226000000' },
        { id: '787', pid: '682', name: '余姚市', code: '330281000000' },
        { id: '810', pid: '682', name: '慈溪市', code: '330282000000' },
        { id: '831', pid: '682', name: '奉化区', code: '330213000000' },
      ],
      street: [
        { id: '882431', name: '南京东路街道', code: '310101002000', pid: '6' },
        { id: '882423', name: '其他', code: '310101002001', pid: '6' },
        { id: '882668', name: '米市巷街道', code: '330105001000', pid: '338' },
        { id: '882667', name: '湖墅街道', code: '330105002000', pid: '338' },
        { id: '882666', name: '小河街道', code: '330105003000', pid: '338', disabled: true },
        { id: '882665', name: '北山街道', code: '330106002000', pid: '339' },
        { id: '882664', name: '西溪街道', code: '330106004000', pid: '339', disabled: true },
      ],
    };
    const getAddress: Address | any = (data: Address_Type, id: string, type: string) => {
      const keys: string[] = ['prov', 'city', 'area', 'street'];
      const key: number = keys.indexOf(type);
      if (data[keys[key - 1]].map((item: Address) => item.code == id).includes(true)) {
        const current = data[keys[key - 1]].filter((item: Address) => item.code == id)[0];
        return getAddress(data, current.id, type);
      } else {
        return data[type].filter((item: Address) => item.pid == id);
      }
    };
    if (id == '0') {
      data = addressMap.prov;
    } else if (
      id == '310000000000' ||
      id == '320000000000' ||
      id == '330000000000' ||
      id == '334' ||
      id == '337' ||
      id == '1'
    ) {
      data = getAddress(addressMap, id, 'city');
    } else if (
      id == '310100000000' ||
      id == '320100000000' ||
      id == '330100000000' ||
      id == '330200000000' ||
      id == '336' ||
      id == '337' ||
      id == '2'
    ) {
      data = getAddress(addressMap, id, 'area');
    } else if (
      id == '310105000000' ||
      id == '330105000000' ||
      id == '330106000000' ||
      id == '320100000000' ||
      id == '338' ||
      id == '6' ||
      id == '339'
    ) {
      data = getAddress(addressMap, id, 'street');
    }

    const result: ResponseItem = {
      code: 0,
      msg: '成功',
      data: data,
    };
    return res.json(result);
  },
};
